package net.zhengxinyun.performance.controller.warn;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.CountryIndicatorsEntity;
import net.zhengxinyun.performance.entity.SixIndicatorsEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.TownIndicatorsEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.SixIndicatorsFilter;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;
import net.zhengxinyun.performance.filter.WarnFilter;
import net.zhengxinyun.performance.service.indicators.CountryIndicatorsService;
import net.zhengxinyun.performance.service.indicators.SixIndicatorsService;
import net.zhengxinyun.performance.service.indicators.TownIndicatorsService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.warn.WarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2019/2/15.
 */
@RestController
@RequestMapping("/warning")
public class WarnController {

    private CountryIndicatorsService countryIndicatorsService;
    private TownIndicatorsService townIndicatorsService;
    private SixIndicatorsService sixIndicatorsService;
    private TokenService tokenService;
    private LoginService loginService;
    @Autowired
    private WarnService warnService;

    @Autowired
    public WarnController(CountryIndicatorsService countryIndicatorsService, TownIndicatorsService townIndicatorsService, SixIndicatorsService sixIndicatorsService, TokenService tokenService, LoginService loginService) {
        this.countryIndicatorsService = countryIndicatorsService;
        this.townIndicatorsService = townIndicatorsService;
        this.sixIndicatorsService = sixIndicatorsService;
        this.tokenService = tokenService;
        this.loginService = loginService;
    }

    @PostMapping("/setCountryEndTime")
    public Result<String> setCountryEndTime(@RequestBody WarnParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = countryIndicatorsService.setEndTime(param.countryIndicatorsEntityList,param.endTime,param.warnType);
            if (!"设置成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults(str);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/setTownEndTime")
    public Result<String> setTownEndTime(@RequestBody WarnParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = townIndicatorsService.setEndTime(param.townIndicatorsEntityList,param.endTime,param.warnType);
            if (!"设置成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/setSixEndTime")
    public Result<String> setSixEndTime(@RequestBody WarnParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = sixIndicatorsService.setEndTime(param.sixIndicatorsEntityList,param.endTime,param.warnType);
            if (!"设置成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/queryForCount")
    public Result<HashMap<String,Object>> queryForCount(@RequestBody WarnParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = (HashMap<String, Object>) warnService.queryCountry(param.warnFilter);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/queryForTown")
    public Result<HashMap<String,Object>> queryForTown(@RequestBody WarnParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = (HashMap<String, Object>) warnService.queryTown(param.warnFilter);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/queryForSix")
    public Result<HashMap<String,Object>> queryForSix(@RequestBody WarnParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = (HashMap<String, Object>) warnService.querySix(param.warnFilter);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }
}
class WarnParam{
    public TokenEntity tokenEntity;
    public CountryIndicatorsEntity countryIndicatorsEntity;
    public List<CountryIndicatorsEntity> countryIndicatorsEntityList;
    public CountryIndicatorsFilter countryIndicatorsFilter;
    public TownIndicatorsEntity townIndicatorsEntity;
    public TownIndicatorsFilter townIndicatorsFilter;
    public SixIndicatorsEntity sixIndicatorsEntity;
    public SixIndicatorsFilter sixIndicatorsFilter;
    public String endTime;
    public List<TownIndicatorsEntity> townIndicatorsEntityList;
    public List<SixIndicatorsEntity> sixIndicatorsEntityList;
    public String warnType;
    public WarnFilter warnFilter;
}
